/*
 * @Description  : 
 * @Author       : zhao
 * @Date         : 2022-07-11 09:16:55
 * @LastEditors  : zhao
 * @LastEditTime : 2022-09-29 12:46:27
 */
import vue from '@vitejs/plugin-vue';
import { resolve } from 'path';
import { defineConfig, loadEnv, ConfigEnv } from 'vite';
// import { viteExternalsPlugin } from "vite-plugin-externals";

// import requireTransform from 'vite-plugin-require-transform';
import OptimizationPersist from 'vite-plugin-optimize-persist';
import PkgConfig from 'vite-plugin-package-config';
const pathResolve = (dir: string): any => {
	return resolve(__dirname, '.', dir);
};

const alias: Record<string, string> = {
	'/@': pathResolve('./src/'),
	'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js',
};

const viteConfig = defineConfig((mode: ConfigEnv) => {
	const env = loadEnv(mode.mode, process.cwd());

	return {
		plugins: [
			PkgConfig(),
			OptimizationPersist(),
			vue({}),
			// requireTransform({
			// 	fileRegex: /.js$|.vue$/
			// }),
		],
		root: process.cwd(),
		resolve: { alias },
		base: "./",
		// base: mode.command === 'serve' ? './' : env.VITE_PUBLIC_PATH,
		optimizeDeps: {
			include: [
				'element-plus/lib/locale/lang/zh-cn',
				'element-plus/lib/locale/lang/en',
				'element-plus/lib/locale/lang/zh-tw',

				'vue',
				'element-plus/es',
				'element-plus/es/components/form/style/index',
				'element-plus/es/components/radio-group/style/index',
				'element-plus/es/components/radio/style/index',
				'element-plus/es/components/checkbox/style/index',
				'element-plus/es/components/checkbox-group/style/index',
				'element-plus/es/components/switch/style/index',
				'element-plus/es/components/time-picker/style/index',
				'element-plus/es/components/date-picker/style/index',
				'element-plus/es/components/col/style/index',
				'element-plus/es/components/form-item/style/index',
				'element-plus/es/components/alert/style/index',
				'element-plus/es/components/breadcrumb/style/index',
				'element-plus/es/components/select/style/index',
				'element-plus/es/components/input/style/index',
				'element-plus/es/components/breadcrumb-item/style/index',
				'element-plus/es/components/tag/style/index',
				'element-plus/es/components/pagination/style/index',
				'element-plus/es/components/table/style/index',
				'element-plus/es/components/table-column/style/index',
				'element-plus/es/components/card/style/index',
				'element-plus/es/components/row/style/index',
				'element-plus/es/components/button/style/index',
				'element-plus/es/components/menu/style/index',
				'element-plus/es/components/sub-menu/style/index',
				'element-plus/es/components/menu-item/style/index',
				'element-plus/es/components/option/style/index',
				'@element-plus/icons-vue',
				'pinia',
				'axios',
				// 'vue-request',
				'vue-router',
				// '@vueuse/core',
			],
		},
		server: {
			hmr: true,
			host: '0.0.0.0',
			port: env.VITE_PORT as unknown as number,
			open: env.VITE_OPEN,
			// open: 'index.html',

			proxy: {
				// '/gitee': {
				// 	target: 'https://gitee.com',
				// 	ws: true,
				// 	changeOrigin: true,
				// 	rewrite: (path) => path.replace(/^\/gitee/, ''),
				// },
				'/api': {
					// target: 'http://175.188.189.182:10081/GeoArcShareInterfaceTryOut',//试运行 后端接口地址
					// target: 'http://10.14.1.18:8033/GeoArcShare',//后端接口地址
					target: 'http://175.188.189.182:10081/GeoArcShareInterface',//后端接口地址
					// target: 'http://192.168.1.158:3592',//长豪
					// target: 'http://192.168.1.218:8122',//程亮iis
					changeOrigin: true,//是否允许跨越
					// ws: true,// 是否代理websockets
					// pathRewrite: {
					// 	'^/api': '/',//重写,  如果后端接口有api '/' | '/api'
					// }
					rewrite: path => path.replace(/^\/api/, '/')
				}
			},
		},
		build: {
			outDir: 'dist',
			sourcemap: false,
			chunkSizeWarningLimit: 1500,
			rollupOptions: {
				output: {
					entryFileNames: `assets/[name].${new Date().getTime()}.js`,
					chunkFileNames: `assets/[name].${new Date().getTime()}.js`,
					assetFileNames: `assets/[name].${new Date().getTime()}.[ext]`,
					compact: true,
					manualChunks: {
						vue: ['vue', 'vue-router', 'pinia'],
						echarts: ['echarts'],
					},
				},
			},
		},
		css: { preprocessorOptions: { css: { charset: false } } },
		define: {
			__VUE_I18N_LEGACY_API__: JSON.stringify(false),
			__VUE_I18N_FULL_INSTALL__: JSON.stringify(false),
			__INTLIFY_PROD_DEVTOOLS__: JSON.stringify(false),
		},
	};
});

export default viteConfig;
